package org.springframework.core.type;


import org.springframework.core.util.AnnotatedElementUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Objects;

public class StandardMethodMetadata implements MethodMetadata {

	private final Method introspectedMethod;

	public StandardMethodMetadata(Method introspectedMethod) {
		Objects.requireNonNull(introspectedMethod, "Method must not be null");
		this.introspectedMethod = introspectedMethod;
	}


	public final Method getIntrospectedMethod() {
		return this.introspectedMethod;
	}

	@Override
	public String getMethodName() {
		return this.introspectedMethod.getName();
	}

	@Override
	public String getDeclaringClassName() {
		return this.introspectedMethod.getDeclaringClass().getName();
	}

	@Override
	public String getReturnTypeName() {
		return this.introspectedMethod.getReturnType().getName();
	}

	@Override
	public boolean isAbstract() {
		return Modifier.isAbstract(this.introspectedMethod.getModifiers());
	}

	@Override
	public boolean isStatic() {
		return Modifier.isStatic(this.introspectedMethod.getModifiers());
	}

	@Override
	public boolean isFinal() {
		return Modifier.isFinal(this.introspectedMethod.getModifiers());
	}

	@Override
	public boolean isOverridable() {
		return (!isStatic() && !isFinal() && !Modifier.isPrivate(this.introspectedMethod.getModifiers()));
	}

	@Override
	public boolean isAnnotated(Class<? extends Annotation> annotationType) {
		return AnnotatedElementUtils.isAnnotated(this.introspectedMethod, annotationType);
	}

	@Override
	public Map<String, Object> getAnnotationAttributes(Class<? extends Annotation> annotationType) {
		return getAnnotationAttributes(annotationType, false);
	}

	@Override
	public Map<String, Object> getAnnotationAttributes(Class<? extends Annotation> annotationType, boolean classValuesAsString) {
		return AnnotatedElementUtils.getAnnotationAttributes(introspectedMethod, annotationType, classValuesAsString);
	}

}
